﻿//-----------------------------------------------------------------------
// <copyright file="DateRouteConstraint.cs" company="OverrideThis">
//     Copyright (c) 2010 OverrideThis.com
// </copyright>
//-----------------------------------------------------------------------
namespace OverrideThis.ContactManager.WebSite.Infrastructure.RouteConstraints
{
    using System;
    using System.Web;
    using System.Web.Routing;

    public class DateRouteConstraint : IRouteConstraint
    {
        private readonly DateRouteConstraintType constraintType;

        public DateRouteConstraint(DateRouteConstraintType constraintType)
        {
            this.constraintType = constraintType;
        }

        #region Implementation of IRouteConstraint

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            var value = int.Parse(values[parameterName].ToString());
            if (constraintType == DateRouteConstraintType.Day)
                return value >= 1 && value <= 31;
            if (constraintType == DateRouteConstraintType.Month)
                return value >= 1 && value <= 12;
            return value >= 1950 && value <= DateTime.Now.Year;
        }

        #endregion
        
        
    }
}